/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.filepicker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import javax.swing.filechooser.FileFilter;
import nl.cloudfarming.client.warmup.CloudfarmingWarmup;
import nl.cloudfarming.sensor.AlreadyParsedException;
import nl.cloudfarming.sensor.FileReadException;
import nl.cloudfarming.util.filecopier.Exception.DirectoryCreationException;
import nl.cloudfarming.util.filecopier.Exception.FileMoveException;
import nl.cloudfarming.util.filecopier.Exception.NotADirectoryException;
import nl.cloudfarming.util.filecopier.Exception.NotAFileException;
import nl.cloudfarming.util.NBPreferencesHolder;
import nl.cloudfarming.util.PreferenceEnum;
import nl.cloudfarming.util.filecopier.ShapeFileCopier;
import org.openide.filesystems.FileChooserBuilder;
import org.openide.util.Exceptions;

public final class ShapeFilePicker extends GenericFilePicker implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        NBPreferencesHolder preferencesHolder = new NBPreferencesHolder();
        String name = preferencesHolder.getProperty(ShapeFilePicker.class, PreferenceEnum.LASTSHAPEDIR);
        File userDir;
        if (!name.equals("")) {
            userDir = new File(name);
        } else {
            userDir = new File(System.getProperty("user.home"));
        }

        File file = new FileChooserBuilder("Shape-directory").setTitle("Choose a directory or file").setDefaultWorkingDirectory(userDir).addFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                String name = f.getName().toLowerCase();
                return f.isDirectory() || (f.isFile() && name.endsWith(".shp"));
            }

            @Override
            public String getDescription() {
                return "Shape file (*.shp)";
            }
        }).setApproveText("Choose").showOpenDialog();

        try {
            if (file != null && file.getName().toLowerCase().endsWith(".shp")) {
                preferencesHolder.saveProperty(ShapeFilePicker.class, PreferenceEnum.LASTSHAPEDIR, file.getPath());
                ShapeFileCopier copier = new ShapeFileCopier();
                String loadString = preferencesHolder.getProperty(CloudfarmingWarmup.class, PreferenceEnum.LOADDIR);

                lockGui();
                if (file.isDirectory()) {
                    File destination = copier.copyDirectory(file, new File(loadString));
                    if (destination.listFiles().length < 1) {
                        throw new FileNotFoundException("Geen files gevonden in geselecteerde folder!");
                    } else {
                        ArrayList<File> shpFileList = new ArrayList<File>();
                        for (File f : destination.listFiles()) {

                            if (f.getName().toLowerCase().endsWith(".shp")) {
                                shpFileList.add(f);
                            }
                        }
                        sendToReceivers(shpFileList);
                    }
                } else {
                    File destination = copier.copyFile(file, new File(loadString));
                    if (destination.getName().toLowerCase().endsWith(".shp")) {
                        sendToReceivers(destination);
                    }
                }
            }
        } catch (NotADirectoryException ex) {
            Exceptions.printStackTrace(ex);
        } catch (FileNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } catch (NotAFileException ex) {
            Exceptions.printStackTrace(ex);
        } catch (DirectoryCreationException ex) {
            Exceptions.printStackTrace(ex);
        } catch (FileMoveException ex) {
            Exceptions.printStackTrace(ex);
        } catch (NoSuchAlgorithmException ex) {
            Exceptions.printStackTrace(ex);
        } catch (FileReadException ex) {
            Exceptions.printStackTrace(ex);
        } catch (AlreadyParsedException ex) {
            Exceptions.printStackTrace(ex);
        } finally{
            freeGui();
        }
    }
}
